import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';

import '../../../../model/form_item_model.dart';
import '../../../../theme/app_colors.dart';
import '../../../../theme/app_text_theme.dart';
import 'loan_controller.dart';


class LoanView extends GetView<LoanController> {
  const LoanView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color_bg,
      appBar: AppBar(title:  Text('我要借款',style: text_title_black)),
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment : CrossAxisAlignment.start,
                children: [
                  Container(
                    width: Get.width - 30.w,
                    margin: EdgeInsets.only(top: 15.w,left: 15.w,right: 15.w),
                    decoration: BoxDecoration(
                      color: Color_white,
                      borderRadius: BorderRadius.all(Radius.circular(8.w)),
                    ),
                    child: Obx(() => Column(
                      crossAxisAlignment : CrossAxisAlignment.start,
                      children: [
                        ...buildLoanMoney(context),
                        ...buildTerm(),
                        ...buildRate(),
                        ...buildInterest(),
                        ...buildBank(),
                        ...buildRepayPlan(),
                      ],
                    )),
                  ),
                  Text('* 温馨提示：费率以机构最终审核结果为准',style: text_131732_14.copyWith(fontSize: 12.sp,color: const Color(0xffffa837))).marginOnly(left: 15.w,top: 9.w),
                ],
              ),
            ),
          ),
          SizedBox(height: 25.w),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 30.w,vertical: 15.w),
            color: Color_white,
            child:  Column(
              children: [
                buildAgreement(),
                RawMaterialButton(
                  fillColor: Color_main,
                  elevation:0,
                  highlightElevation:0,
                  shape : RoundedRectangleBorder(borderRadius : BorderRadius.all(Radius.circular(22.w))),
                  constraints: BoxConstraints.tightFor(width: Get.width - 60.w, height:44.w),
                  child: Text('下一步',style: text_white_14),
                  onPressed: ()=> controller.next(),
                ),
              ],
            )),
        ],
      ) ,
    );
  }

  List<Widget> buildLoanMoney(BuildContext context) {
    if(controller.loanMoney.value == 0 || controller.canLoanMin.value == 0 || controller.canLoanMax.value == 0){
      return [Container()];
    } else {
      return [
        Text('贷款额度：',style: text_131732_14).marginOnly(left: 15.w,top: 12.w),
        SizedBox(height: 6.w),
        Center(child: Obx(()=>Text('${controller.loanMoney.value.ceil()}',style: text_131732_14.copyWith(fontSize: 28.sp)))),
        Stack(children: [
          Obx(() => SliderTheme(
            data: SliderTheme.of(context).copyWith(
              overlayShape: RoundSliderOverlayShape(overlayRadius: 16.w), // 调整滑块覆盖层的大小
              tickMarkShape: RoundSliderTickMarkShape(tickMarkRadius: 4.w), // 调整刻度标记的大小
              overlayColor: Color_main.withOpacity(0.2), // 设置滑块覆盖层的颜色和透明度
              activeTrackColor: Color_main, // 设置活动进度条的颜色
              inactiveTrackColor: Color_line, // 设置非活动进度条的颜色
              thumbColor: Color_main, // 设置滑块的颜色
            ),
            child: Slider(
              value: controller.loanMoney.value,
              onChanged: (value)=> controller.loanMoney.value = value,
              onChangeEnd: (value)=> controller.trial(),
              min: controller.canLoanMin.value,
              max: controller.canLoanMax.value,
              divisions: (controller.canLoanMax.value - controller.canLoanMin.value) ~/ 1000,
            ),
          )),
          Row(
            mainAxisAlignment : MainAxisAlignment.spaceBetween,
            children: [
              Obx(() => Text('${controller.canLoanMin.value.ceil()}',style: text_grey_12).marginOnly(left: 5.w,top: 25.w)),
              Obx(() => Text('${controller.canLoanMax.value.floor()}',style: text_grey_12).marginOnly(right: 5.w,top: 25.w)),
            ],
          )
        ]),
        SizedBox(height: 6.w),
      ];
    }
  }
  List<Widget> buildTerm() {
    if(controller.termList.isEmpty){
      return [Container()];
    } else {
      return [
        Text('贷款期限',style: text_131732_14).marginOnly(left: 15.w,top: 12.w),
        Obx(() => CustomScrollView(
          shrinkWrap:true,
          slivers: [
            SliverGrid(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: controller.termList.isNotEmpty ? controller.termList.length : 4,
                  mainAxisSpacing : 10.w,
                  crossAxisSpacing : 10.w,
                  childAspectRatio : (Get.width -60.w - (controller.termList.length-1)*10.w) / controller.termList.length / 30.w
              ),
              delegate: SliverChildBuilderDelegate((context, index) {
                var bean = controller.termList[index];
                return RawMaterialButton(
                    elevation: 0,
                    fillColor:bean.select ?Color_main:Color_bg,
                    highlightElevation : 0,
                    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.w)),
                    constraints: BoxConstraints.tightFor(width: (Get.width -60.w - (controller.termList.length-1)*10.w) / controller.termList.length, height: 30.w),
                    child: Text('${bean.name}',style: text_131732_12.copyWith(color: bean.select?Color_white:Color_131732)),
                    onPressed: ()=> controller.selectTerm(index)
                );
              },
                childCount: controller.termList.length,
              ),
            ),
          ],
        ).marginOnly(left: 15.w,right: 15.w,top: 12.w)),
        SizedBox(height: 12.w),
      ];
    }
  }
  List<Widget> buildRate() {
    if(controller.rate.value == 0){
      return [Container()];
    } else {
      return [
        Container(height: 0.2,color: Color_line),
        Row(children: [
          Text('年化利率　　',style: text_131732_14),
          Text('${controller.rate}%',style: text_131732_14),
        ]).marginOnly(left: 15.w,top: 12.w),
        SizedBox(height: 12.w),
      ];
    }
  }
  List<Widget> buildInterest() {
    if(controller.interest.value == 0){
      return [Container()];
    } else {
      return [
        Container(height: 0.2,color: Color_line),
        Row(children: [
          Text('总利息　　　',style: text_131732_14),
          Text('${controller.interest}元',style: text_131732_14),
        ]).marginOnly(left: 15.w,top: 12.w),
        SizedBox(height: 12.w),
      ];
    }
  }
  List<Widget> buildBank() {
    if(!controller.isFullApi.value){
      return [Container()];
    } else {
      var haveSelectBank = controller.selectBank.value.no.isNotEmpty;
      var str = haveSelectBank?'${controller.selectBank.value.name} (${controller.selectBank.value.no.substring(controller.selectBank.value.no.length - 4,controller.selectBank.value.no.length)})':'';
      return [
        Container(height: 0.2,color: Color_line),
        InkWell(
          onTap: () => controller.chooseBank(),
          child: Row(children: [
            Text('银行卡　　　',style: text_131732_14),
            Expanded(child: Text(haveSelectBank?str:'请绑定银行卡',style: haveSelectBank?text_131732_14:text_grey_14)),
            SizedBox(width: 6.w),
            Image.asset('assets/images/ic_next.png',width: 15.w,height: 15.w,color: Color_grey),
            SizedBox(width: 15.w),
          ]).marginOnly(left: 15.w,top: 12.w),
        ),
        SizedBox(height: 12.w),
      ];
    }
  }
  List<Widget> buildRepayPlan() {
    if(controller.repayPlanList.isEmpty){
      return [Container()];
    } else {
      return [
        Container(height: 0.2,color: Color_line),
        InkWell(
          onTap: () => controller.repayPlan(),
          child: Row(children: [
            Text('还款计划　　',style: text_131732_14),
            Expanded(child: Text('点击查看还款计划',style: text_131732_14)),
            SizedBox(width: 6.w),
            Image.asset('assets/images/ic_next.png',width: 15.w,height: 15.w,color: Color_grey),
            SizedBox(width: 15.w),
          ]).marginOnly(left: 15.w,top: 12.w),
        ),
        SizedBox(height: 12.w)
      ];
    }
  }
  Widget buildAgreement(){
    if(controller.agreementList.isEmpty){
      return Container();
    }else{
      return Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          InkWell(
            highlightColor: Colors.transparent, // 透明色
            splashColor: Colors.transparent, // 透明色
            onTap: (){
              controller.isSelectAgreement.value = !controller.isSelectAgreement.value;
            },
            child: Obx(()=>Image.asset(controller.isSelectAgreement.value ? "assets/images/ic_check_p.png" : "assets/images/ic_check_n.png",width: 25.w,height: 25.w)),
          ),
          RichText(
            text: TextSpan(
              style: text_grey_12,
              children: [
                const TextSpan(text: '我已阅读并同意'),
                TextSpan(text: '《个人单笔消费借款合同》', style: text_main_12, recognizer: TapGestureRecognizer()..onTap = () => controller.toAgreementList() ),
              ],
            ),
          ).marginOnly(bottom: 2.w)
        ],
      ).marginOnly(bottom: 6.w);
    }
  }
}
